<template>
  <div class="goodsinfo">
    <!-- 小球 -->
    <transition @before-enter="before" @enter="enter" @after-enter="afterenter">
      <div class="ball" v-show="flag" ref="ball"></div>
    </transition>
    <!-- 轮播图 -->
    <div class="mui-card">
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <swipe :lunbotuList="lunbotuList" :isfull="false"></swipe>
        </div>
      </div>
    </div>
    <!-- 商品购买 -->
    <div class="mui-card">
      <!--页眉，放置标题-->
      <div class="mui-card-header">{{goodsinfo.title}}</div>
      <!--内容区-->
      <div class="mui-card-content">
        <div class="price">
          市场价
          <span class="old">{{goodsinfo.market_price}}</span>
          销售价
          <span class="news">{{goodsinfo.sell_price}}</span>
        </div>
        <div class="account">购买数量
          <numbox :max="goodsinfo.stock_quantity" @getcount="getselectedCount"></numbox>
        </div>
        <div>
          <mt-button type="primary" size="small">立即购买</mt-button>
          <mt-button type="danger" size="small" @click="addToShopCar">加入购物车</mt-button>
        </div>
      </div>
    </div>
    <!-- 商品参数 -->
    <div class="mui-card">
      <!--页眉，放置标题-->
      <div class="mui-card-header">商品参数</div>
      <!--内容区-->
      <div class="mui-card-content">
        <p>商品货号{{goodsinfo.goods_no}}</p>
        <p>库存数量{{goodsinfo.stock_quantity}}</p>
        <p>上架时间{{goodsinfo.add_time|dateformat}}</p>
      </div>
      <!--页脚，放置补充信息或支持的操作-->
      <div class="mui-card-footer">
        <mt-button size="large" type="primary" plain @click="godesc(id)">图文介绍</mt-button>
        <mt-button size="large" type="danger" plain @click="gocomment(id)">商品评论</mt-button>
      </div>
    </div>
  </div>
</template>

<script>
import swipe from "../subcomponents/swiper.vue";
import numbox from "../subcomponents/goodsinfo_numbox.vue";
export default {
  data() {
    return {
      id: this.$route.params.id,
      lunbotuList: [],
      goodsinfo: {},
      flag: false,
      selectedCount: 1
    };
  },
  created() {
    this.getlunbotu();
    this.getGoodsInfo();
  },
  methods: {
    getlunbotu() {
      this.$http.get("api/getthumimages/" + this.id).then(result => {
        if (result.body.status == 0) {
          result.body.message.forEach(item => {
            item.img_url = item.src;
          });
          this.lunbotuList = result.body.message;
        }
      });
    },
    getGoodsInfo() {
      // 获取商品的信息
      this.$http.get("api/goods/getinfo/" + this.id).then(result => {
        if (result.body.status === 0) {
          this.goodsinfo = result.body.message[0];
        }
      });
    },
    godesc(id) {
      this.$router.push({ name: "goodsdesc", params: { id } });
    },
    gocomment(id) {
      this.$router.push({ name: "goodscomment", params: { id } });
    },
    getselectedCount(count) {
      //获取添加的数量
      this.selectedCount = count;
      console.log("父组件拿到的数量值为： " + this.selectedCount);
    },
    addToShopCar() {
      this.flag = !this.flag;
      var goodsinfo = {
        id: this.id,
        count: this.selectedCount,
        price: this.goodsinfo.sell_price,
        selected: true
      };
      this.$store.commit("addToCar", goodsinfo);
    },
    before(el) {
      el.style.transform = "translate(0, 0)";
    },
    enter(el, done) {
      el.offsetWidth;
      //获取小球在页面中的位置
      var ballposition = this.$refs.ball.getBoundingClientRect();
      //获取徽章在页面中的位置
      var badgeposition = document
        .getElementById("mui-badge")
        .getBoundingClientRect();
      var left = badgeposition.left - ballposition.left;
      var top = badgeposition.top - ballposition.top;
      el.style.transform = `translate(${left}px, ${top}px)`;
      el.style.transition = "all 1s ease";

      done();
    },
    afterenter() {
      this.flag = !this.flag;
    }
  },
  components: {
    swipe,
    numbox
  }
};
</script>

<style lang="scss" scope>
.goodsinfo {
  padding-bottom: 50px;
}
.account {
  margin: 10px 0;
}
.ball {
  width: 15px;
  height: 15px;
  border-radius: 50%;
  background: #f40;
  position: absolute;
  z-index: 999;
  top: 350px;
  left: 120px;
}
</style>
